clear; clc
v = 200;
[t,y]=ode45(@df ,[0,0.1],[0 0]); 
x = y(:,1);  y =y(:,2);
plot(x, y,'r*','MarkerSize',1)  
hold on
plot([20,30],[0,10],'b-') 
hold on
% 找到导弹与B船相撞的点
n =length(t);  
for i = 1:n 
    d = t(i) * v * 3;  
    dd =sqrt((x(i)-(20+sqrt(2)/2*v*t(i)))^2+(y(i)-sqrt(2)/2*v*t(i))^2);  
    if dd < 0.01 
        if d <= 50 
            disp(['导弹飞行',num2str(d),'单位后击中B船'])
            disp(['导弹飞行的时间为',num2str(t(i)*60 ),'分钟']) 
            disp('击中点的坐标：');  disp([x(i),y(i)]) 
            plot(x(i),y(i),'r*');
            text(x(i)+0.5,y(i)+0.1,'击中点')
        end
        break; 
    end
end
if d >50 || dd >= 0.01 
    disp('导弹没有击中B船');
end

function dy=df(t,y)
    v = 200; 
    dy=zeros(2,1);
    dy(1)=3*v*(20+sqrt(2)/2*v*t-y(1))/sqrt((20+sqrt(2)/2*v*t-y(1))^2+(sqrt(2)/2*v*t-y(2))^2);
    dy(2)=3*v*(sqrt(2)/2*v*t-y(2))/sqrt((20+sqrt(2)/2*v*t-y(1))^2+(sqrt(2)/2*v*t-y(2))^2);
end
